<?php

if (!defined('IN_PHPBB'))
{
	exit;
}

if (!function_exists('get_tags'))
{

	function get_tags($classname, $uniqueid, $edit = false)
	{

		global $db;
		global $table_prefix;
		global $user;

		//are we logged in?
		$logged_in = ($user->data[user_id] > 1);

		$moderator = is_moderator();

		//create return variable
		$res = '';

		$sql = 'select tr.object_class, tr.object_id, t.name ,t.slug, tr.term_taxonomy_id, tr.user_id ';
		$sql = $sql . 'from ' . $table_prefix . 'colauth_term_relationships tr ';    
		$sql = $sql . 'join ' . $table_prefix . 'colauth_term_taxonomy tt on tr.term_taxonomy_id = tt.term_taxonomy_id ';    
		$sql = $sql . 'join ' . $table_prefix . 'colauth_terms t on t.term_id = tt.term_id ';    
		$sql = $sql . 'where tr.object_class = "' . $db->sql_escape($classname) . '"';    
		$sql = $sql . 'and tr.object_id = ' . $db->sql_escape($uniqueid) ;    

		$result = $db->sql_query($sql);
		$rowset = $db->sql_fetchrowset($result);

		foreach ($rowset as $row)
		{
			// [0] = object_class
			// [1] = object_id
			// [2] = name
			// [3] = slug
			// [4] = term_taxonomy_id
			// [5] = user_id

			//swap the tags over for links if we're not editing
			if($edit)
			{
				$res = $res . '<div class="colauth_term">' . $row[name];
			}
			else 
			{
				$res = $res . '<a class="colauth_term" href="search.php?search_id=tag&amp;sr=topics&amp;taxonomy=' . $row[term_taxonomy_id] . '">' . $row[name];
			}

			//if we're in edit mode AND we added the tag in the first
			//place, add the remove link for it.
			$same_user = ($row[user_id] == $user->data[user_id]);
			if(($edit && $same_user) || ($edit && $moderator))
			{
				//$res = $res . ' [x]';
				$res = $res . ' [<input class="colauth_remove_term_button" type="submit" name="cmdRemoveTag#'. $row[term_taxonomy_id] .'" value="x" />]&nbsp;';
			}
			unset($same_user);

			// close the tags
			if($edit)
			{
				$res = $res . '</div>';
			}
			else
			{
				$res = $res . '</a>';
			}

		}

		//handle no tags
		if (!$res)
		{
			$res = $res . '<div class="colauth_empty_terms">No Tags</div>';
		}


		//if we're editing, we need to include the "Add" functions...
		if ($edit)
		{
			$res = $res . '<input class="inputbox tiny" type="text" name="txtAddTag"/>';
			$res = $res . '&nbsp;<input class="button2" type="submit" name="cmdAddTag" id="btnAddTag" value="Add Tag" form="frm-tags"/>';
		}
		else
		{
			//if we're not editing, add the button to switch modes.
			// ONLY DO THIS IF WE'RE LOGGED IN
			if ($logged_in)
			{
				$res = $res . '&nbsp;<input class="button2" type="submit" name="cmdEditTags" id="btnEditTags" value="Edit Tags"/>';
			}
		}


		//add tagbox header
		$res = '<form method="post" id="frm-tags"><div class="colauth_term_heading">Tags:</div>' . $res . '</form>';

		//tidying up
		$db->sql_freeresult($result);
		unset($sql);
		unset($result);
		unset($logged_in);
		unset($moderator);

		return $res;
	}

}

if (!function_exists('add_topic_tag'))
{

	function add_topic_tag($classname, $uniqueid, $tag, $userid)
	{

		global $db;
		global $table_prefix;
		global $user;

		$slug = get_slug($tag);
		$taxonomy = 0;

		//are we logged in?
		$logged_in = ($user->data[user_id] > 1);

		//only proceed if we're logged in
		if (!$logged_in)
		{
			exit('Invalid operation');
		}

		//before we do anything, kick off a transaction as there's
		//potentially quite a few insert/update statements here...
		$db->sql_transaction('begin');

		//check whether the tag already exists...
		$sql = '';
		$sql = $sql . 'select tt.term_taxonomy_id, t.name ,t.slug ';
		$sql = $sql . 'from ' . $table_prefix . 'colauth_term_taxonomy tt ';
		$sql = $sql . 'join ' . $table_prefix . 'colauth_terms t on t.term_id = tt.term_id ';
		$sql = $sql . 'where t.slug = "' . $db->sql_escape($slug) . '"';

		$qrySlugCheck = $db->sql_query($sql);
		$resSlugCheck = $db->sql_fetchrow($qrySlugCheck);
		if ($resSlugCheck)
		{
				$taxonomy = $resSlugCheck[term_taxonomy_id];
		}

		try{
			$db->sql_freeresult();
		} catch(exception $e){

		};

		//check to see if we need additional steps for a new term...
		if (!$taxonomy)
		{

			//create a variable to hold the new term Id
			$newTermID = 0;

			//add the basic term to the dictionary
			$sql = '';
			$sql = $sql . 'insert into ' . $table_prefix . 'colauth_terms(name,slug,term_group) ';
			$sql = $sql . 'values("' . $db->sql_escape($tag) . '","' . $db->sql_escape($slug) . '",0)';
			$db->sql_query($sql);

			//get the term_id we just added
			$sql = '';
			$sql = $sql . 'select t.term_id ';
			$sql = $sql . 'from ' . $table_prefix . 'colauth_terms t ';
			$sql = $sql . 'where t.slug = "' . $db->sql_escape($slug) . '"';
			$db->sql_query($sql);
			$resNewSlug = $db->sql_fetchrow();

			$newTermID = $resNewSlug[term_id];

			try
			{
				$db->sql_freeresult();
			}
			catch (exception $e){

			};

			// add a new taxonomy...
			$sql = '';
			$sql = $sql . 'insert into ' . $table_prefix . 'colauth_term_taxonomy(term_id, taxonomy, description, parent, count) ';
			$sql = $sql . 'values(' . $db->sql_escape($newTermID) . ',"bb-topic-tag","",0,0)';
			$db->sql_query($sql);

			// get the new taxonomy id...
			$sql = '';
			$sql = $sql . 'select term_taxonomy_id ';
			$sql = $sql . 'from ' . $table_prefix . 'colauth_term_taxonomy ';
			$sql = $sql . 'where term_id = ' . $db->sql_escape($newTermID);
			$db->sql_query($sql);
			$resNewTerm = $db->sql_fetchrow();

			$taxonomy = $resNewTerm[term_taxonomy_id];

			try
			{
				$db->sql_freeresult();
			}
			catch (exception $e){

			};

			unset($newTermID);

		}

		// we should have a valid taxonomy at this point... 

		//insert relationship
		$sql = '';
		$sql = $sql . 'insert into ' . $table_prefix . 'colauth_term_relationships';
		$sql = $sql . '(object_class, object_id, term_taxonomy_id, user_id, term_order) ';
		$sql = $sql . 'values("' . $db->sql_escape($classname) . '",' . $db->sql_escape($uniqueid) . ',' . $db->sql_escape($taxonomy) . ',' . $db->sql_escape($userid) . ',0)';
		$db->sql_query($sql);

		//increment taxonomy count
		$sql = '';
		$sql = $sql . 'update ' . $table_prefix . 'colauth_term_taxonomy ';
		$sql = $sql . 'set count = (count + 1) ';
		$sql = $sql . 'where term_taxonomy_id = ' . $db->sql_escape($taxonomy);
		$db->sql_query($sql);

		$db->sql_transaction('commit');

		//tidying up
		unset($sql);
		unset($slug);
		unset($taxonomy);

	}

}


if(!function_exists('remove_topic_tag'))
{

	function remove_topic_tag($classname, $uniqueid, $taxonomyid, $userid)
	{
		global $db;
		global $table_prefix;
		global $user;

		//are we logged in?
		$logged_in = ($user->data[user_id] > 1);

		$moderator = is_moderator();

		if (!$logged_in)
		{
			exit('Invalid operation');
		}


		//does this tag belong to this user?
		$sql = '';
		$sql = $sql . 'select user_id ';
		$sql = $sql . 'from ' . $table_prefix . 'colauth_term_relationships ';
		$sql = $sql . 'where object_class = "' . $db->sql_escape($classname) . '" ';
		$sql = $sql . 'and object_id = ' . $db->sql_escape($uniqueid) . ' ';
		$sql = $sql . 'and term_taxonomy_id = ' . $db->sql_escape($taxonomyid);

		$db->sql_query($sql);

		$res_user_id = $db->sql_fetchrow();
		$rel_user_id = $res_user_id[user_id];

		try
		{
			$db->sql_freeresult();
		}
		catch (exception $e){

		};

		//check to see that the user_id matches the logged in user
		// OR it's a moderator / administrator...
		if (($rel_user_id == $user->data[user_id]) || ($moderator))
		{
			// start the transaction
			$db->sql_transaction('begin');

			//proceed if the user_ids match.
			$sql = '';
			$sql = $sql . 'delete from ' . $table_prefix . 'colauth_term_relationships ';
			$sql = $sql . 'where object_class = "' . $db->sql_escape($classname) . '" ';
			$sql = $sql . 'and object_id = ' . $db->sql_escape($uniqueid) . ' ';
			$sql = $sql . 'and term_taxonomy_id = ' . $db->sql_escape($taxonomyid);
			$db->sql_query($sql);

			// finish the transaction
			$db->sql_transaction('commit');
		}
		else
		{
			//not the right user - quit.
			exit('Invalid operation');
		}

		unset($sql);
		unset($moderator);

	}

}

if (!function_exists('get_slug'))
{

	function get_slug($term)
	{
		$term = strtolower($term);
		$term = str_replace(' ','-',$term);
		return $term;
	}

}


if (!function_exists('get_tag_name'))
{

	function get_tag_name($taxonomy)
	{
		global $db;
		global $table_prefix;

		$str = '';

		$sql = '';
		$sql = $sql . 'select trm.name ';
		$sql = $sql . 'from ' . $table_prefix . 'colauth_term_taxonomy tt ';
		$sql = $sql . 'join ' . $table_prefix . 'colauth_terms trm on tt.term_id = trm.term_id ';
		$sql = $sql . 'where tt.term_taxonomy_id = ' . $db->sql_escape($taxonomy);

		$res = $db->sql_query($sql);
		$row = $db->sql_fetchrow($res);

		$str = $row[name];

		$db->sql_freeresult($res);

		unset($res);
		unset($row);
		unset($sql);

		return $str;

	}

}


if (!function_exists('get_tag_mode'))
{

	function get_tag_mode($topic_id)
	{
		global $user;

		// this is the main calculation section for tags...
		$tag_edit_mode = false;
		$tag_editing = request_var('cmdEditTags','');
		$tag_adding = request_var('cmdAddTag','');
		$tag_new_value = request_var('txtAddTag','');
		$tag_removing = false;
		$tag_to_remove = false;
		// check through the posted keys to see if there's a remove instruction
		foreach($_POST as $key => $submitted_value)
		{
			if(($submitted_value == 'x'))
			{
				$split_key = explode('#',$key);
				if($split_key[0] == 'cmdRemoveTag')
				{
					$tag_removing = true;
					$tag_to_remove = $split_key[1];
				}
				unset($split_key);
			}
		}

		if ($tag_editing)
		{
			$tag_edit_mode = true;
		}
		else if ($tag_adding)
		{
			add_topic_tag('topic',$topic_id, $tag_new_value, $user->data['user_id']);
		}
		else if ($tag_removing)
		{
			remove_topic_tag('topic', $topic_id, $tag_to_remove, $user->data['user_id']);
		}

		unset($tag_adding, $tag_editing, $tag_new_value, $tag_to_remove);

		return $tag_edit_mode;
	}

}

if (!function_exists('is_moderator'))
{

	function is_moderator()
	{
		global $user;
		global $db;
		global $table_prefix;

		$sql = 'select group_name from ' . $table_prefix . 'groups where group_id = ' . $db->sql_escape($user->data[group_id]);
		$db->sql_query($sql);
		$res = $db->sql_fetchrow();
		$group_name = $res[group_name];
		$is_mod = ($group_name == 'GLOBAL_MODERATORS' || $group_name == 'ADMINISTRATORS');

		try
		{
			$db->sql_freeresult();
		}
		catch(exception $e)
		{

		};

		unset($res);
		unset($group_name);    
		return $is_mod;
	}

}

?>
